<?php
	$_t = microtime(true);

	#just in case we need to check perms or re-direct after we start compiling
	ob_start(); //if output_buffering is already on, then you can just comment out this line.

	#disable caching
  header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
	
	#headers for cool people
  header('Server: www.servoso.com');
	header('X-Powered-By: www.phpdriver.org');
	
	#we gotta have a session
	session_start();

  #get the config
  require_once 'config.php';
  
  #load the functions, also see functions/_.php for the __autoload function (for classes)
  foreach (scandir(PHPDRIVER . 'functions') as $file) {
  	if ($file == '.' || $file == '..' || substr($file, strrpos($file, '.')) != '.php') continue;
    
    require_once PHPDRIVER . "functions/$file";
  }

  #load the conf from the database
  $sql = "SELECT * FROM `conf`";
  $qry = db_query($sql);
  while ($conf = db_fetch_assoc($qry)) {
		$val = trim($conf['value']);
		if ($val === 'true') $val = true;
		elseif ($val === 'false') $val = false;
		
  	define(strtoupper(str_replace(' ', '_', $conf['name'])), $val);
  }
	
	#auto install tables from database_schema/{table_name}.ini
	db_install_tables();
  
	#seo redirect when neccessary
	if (HOSTNAME != 'www.localhost.localdomain' && $_SERVER['HTTP_HOST'] != HOSTNAME) {
  	header( "HTTP/1.1 301 Moved Permanently" );
  	location('http' . (($_SERVER['HTTPS']) ? 's' : '') . '://' . CONF_HOSTNAME);
	} 
	
  #reload the current user (we are paranoid; so on every page refresh, we load the user again.)
  load_current_user(); //loads the currently logged in user (and their perms schema) into the varable: $_SESSION['current_user']
  //printr($_SESSION['current_user']);//for debugging
	
	#mod rewrite urls
	load_uri();
	
	#load fckeditor
	include PHPDRIVER . 'plugins/fckeditor/fckeditor.php';